package com.ws.universal.tools.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.gyf.barlibrary.ImmersionBar;
import com.ws.universal.tools.R;
import com.ws.universal.tools.permission.SettingService;
import com.ws.universal.tools.utils.LanguageUtils;
import com.ws.universal.tools.utils.LocationUtils;


public abstract class BaseActivity extends AppCompatActivity {
    protected final int TEXT_PASSWORD = 0x00000081;//密码
    protected final int TEXT_NUMBER = 0x00000002;//数字
    protected final int TEXT_TEXT = 0x00000001;//文字
    public SettingService mSettingService;
    public int page_size = 10;
    protected BaseProgressDialog mProgressDialog;
    private Context context;
    private Activity activity;
    private Location location;

    /*else if (303 == commonality.getStatusCode()) {
       upDateApp(object.optString("url"), object.optString("msg"), false);
   } else if (401 == commonality.getStatusCode()) {
       CacheUtils.setString(Constants.token, "");
       CacheUtils.setBoolean(Constants.isLogin, false);
       Intent intent = new Intent(ManageActivity.getInstance().getCurrentActivity(), LoginActivity.class);
       intent.putExtra("isRegist", false);
       ManageActivity.getInstance().getCurrentActivity().startActivity(intent);
   }*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //沉浸式
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .statusBarColor(R.color.transparent)
                .fitsSystemWindows(false)  //使用该属性必须指定状态栏的颜色，不然状态栏透明，很难看
                .init();
        context = this;
        activity = this;
        mProgressDialog = new BaseProgressDialog(context);
        initCreate(savedInstanceState);
        initView();
    }

    public Activity getActivity() {
        return activity;
    }

    public Location getLocation() {
        return LocationUtils.getInstance().getLocation();
    }

    protected abstract void initCreate(Bundle savedInstanceState);

    protected abstract void initView();

    protected abstract void FinishDesTroy();

    public Context getContext() {
        return context;
    }

    public void showProgressDialog(boolean cancelable) {
        showProgressDialog(cancelable, getResources().getString(R.string.loading));
    }

    public void showProgressDialog(boolean cancelable, String msg) {
        showProgressDialog(null, cancelable, msg);
    }

    public void showProgressDialog(BaseProgressDialog.OnCancelListener cancelListener, boolean cancelable, String msg) {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            return;
        }
        mProgressDialog.setMessage(msg);

        if (cancelListener != null) {
            mProgressDialog.setOnCancelListener(cancelListener);
        }
        mProgressDialog.setCancelable(cancelable);
        mProgressDialog.show();
    }

    public void stopProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.stop();
        }
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        LanguageUtils.attachBaseContext(newBase);
    }


    @Override
    public void onBackPressed() {
        FinishDesTroy();
        super.onBackPressed();
    }

    /**
     * 不带参数的跳转
     *
     * @param clazz 跳转到的目标类
     */
    protected void pageJump(final Class<?> clazz) {
        Intent intent = new Intent(getContext(), clazz);
        startActivity(intent);
    }

    /**
     * 带参数的跳转
     *
     * @param clazz  跳转到的目标类
     * @param bundle 参数
     */
    protected void pageJump(final Class<?> clazz, final Bundle bundle) {
        Intent intent = new Intent(getContext(), clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 跳转然后退出当前页
     *
     * @param clazz 跳转到的目标类
     */
    protected void pageJumpKill(final Class<?> clazz) {
        Intent intent = new Intent(getContext(), clazz);
        startActivity(intent);
        finish();
    }

    /**
     * 带参数跳转然后退出当前页
     *
     * @param clazz  跳转到的目标类
     * @param bundle 参数
     */
    protected void pageJumpKill(final Class<?> clazz, final Bundle bundle) {
        Intent intent = new Intent(getContext(), clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        finish();
    }

    /***
     * 隐藏键盘
     * @param mEtPassword
     */
    public void hideSoftInput(View mEtPassword) {
        mEtPassword.clearFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mEtPassword.getWindowToken(), 0); //强制隐藏键盘
    }

    /***
     * 显示键盘
     * @param editText
     */
    public void showSoftInput(EditText editText) {
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        editText.setSelection(editText.getText().length());
        InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInput(editText, 0);
        }
    }

    /**
     * 使用示例
     * Intent intent = new Intent(getContext(), GrowthCollegeDownListActivity.class);
     * startActivity(intent);
     * overridePendingTransition(R.anim.anim_slide_bottom_in, R.anim.anim_alpha_out);
     *
     * @param enterAnim
     * @param exitAnim
     */
    public void overridePageAmin(int enterAnim, int exitAnim) {
        overridePendingTransition(enterAnim, exitAnim);
    }

}
